﻿using System.Collections.Generic;
using System.Diagnostics;
using System.Xml.Linq;

namespace LoadTestDbToXml
{
    static class CountersReader
    {
        public static List<CounterGroup> Read(string fileName)
        {
            var groups = new List<CounterGroup>();
            XElement allData = XElement.Load(fileName);
            IEnumerable<XElement> counterGroups = allData.Descendants("CounterGroup");
            foreach (XElement counterGroup in counterGroups)
            {
                Debug.Assert(counterGroup != null, "counterGroup != null");

                IEnumerable<XElement> machineElements = counterGroup.Descendants("Machine");
                var machines = new List<string>();
                foreach (var machineElement in machineElements)
                {
                    var machineName = machineElement.Value;
                    machines.Add(machineName);
                }

                string groupName = counterGroup.Attribute("Name").Value;
                IEnumerable<XElement> counterElements = counterGroup.Descendants("Counter");
                List<Counter> counters = new List<Counter>();
                foreach (XElement counter in counterElements)
                {
                    string counterName = counter.Attribute("CounterName").Value;
                    string instanceName = counter.Attribute("InstanceName").Value;
                    counters.Add(new Counter(counterName, instanceName));
                }

                groups.Add(new CounterGroup(groupName, machines, counters));
            }

            return groups;
        }
    }

    internal class CounterGroup
    {
        private readonly string name;
        private readonly List<string> machines;
        private readonly List<Counter> counters;

        public CounterGroup(string name, List<string> machines, List<Counter> counters)
        {
            this.name = name;
            this.machines = machines;
            this.counters = counters;
        }

        public IEnumerable<string> Machines
        {
            get { return machines; }
        }

        public string Name
        {
            get { return name; }
        }

        public List<Counter> Counters
        {
            get { return counters; }
        }
    }

    public class Counter
    {
        private readonly string counterName;
        private readonly string instanceName;

        public Counter(string counterName, string instanceName)
        {
            this.counterName = counterName;
            this.instanceName = instanceName;
        }

        public string CounterName
        {
            get { return counterName; }
        }

        public string InstanceName
        {
            get { return instanceName; }
        }
    }
}
